/**
 * 
 */
package com.et114.core.control;

//import org.acegisecurity.vote.AccessDecisionVoter;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean ;
import org.springframework.util.Assert;

import com.et114.core.common.AppContext;
import com.et114.core.common.CONST;
import com.et114.core.common.FrameWorkPropertiesBean;
import com.et114.core.utility.ConfigUtil;

import java.util.Collection;
import java.util.Iterator;

/**
 * @author guanhw
 */
public class AppInitBean implements InitializingBean {
	private static Log logger = LogFactory.getLog( AppInitBean.class );
	private Collection initBeans = null ; 
	public AppInitBean ( ) {
		// TODO Auto-generated constructor stub
	}

	public void afterPropertiesSet ( ) throws Exception {
		// TODO Auto-generated method stub
		logger.debug(  "init start ...... " ) ;
		initAppConfig() ; 
		initoBeans() ;
		logger.debug(  "init end ...... " ) ;
	}
	
	
	private   void initAppConfig() throws Exception {
		logger.debug( "init app config start ..."  ) ;
		FrameWorkPropertiesBean fwpb = AppContext.getFrameWorkPropertiesBean() ;
		try {
			Configuration config = ConfigUtil.getConfig() ;
			if ( config.containsKey(  FrameWorkPropertiesBean.DB_TYPE_KEY  ) ) {
				String dbType = config.getString(  FrameWorkPropertiesBean.DB_TYPE_KEY  ) ;
				if ( dbType == null || "".equals(  dbType  ) ) throw new Exception ( "config.properties::db.type " ) ;
				if ( !dbType.toUpperCase().equals(   CONST.DB_ORACLE  ) && 
						!dbType.toUpperCase().equals(  CONST.DB_SQLSERVER  ) &&
						!dbType.toUpperCase().equals(  CONST.DB_MYSQL  ) )
					throw  new Exception ( "config.properties::db.type ORACLE , SQLSERVER , MYSQL !" ) ;
				
				fwpb.setDbType(   dbType  ) ; 
			}
			
			
			int  maxThread = 10 ;
			if ( config.containsKey(  FrameWorkPropertiesBean.DOWNLOAD_MAX_THREAD    ) ) {
				maxThread = config.getInt(  FrameWorkPropertiesBean.DOWNLOAD_MAX_THREAD ) ; 
			}
			
			fwpb.setDownloadMaxThread(  maxThread  ) ;
			
			String charSet = "UTF-8" ; 
			if ( config.containsKey(  FrameWorkPropertiesBean.APP_CHARSET  ) ) {
				charSet = config.getString(  FrameWorkPropertiesBean.APP_CHARSET   ) ;
			}
						
			fwpb.setCharSet(  charSet  ) ;
			
			String dataProvider = "database" ; 
			if ( config.containsKey( FrameWorkPropertiesBean.DATA_PROVIDER ) ) {
				dataProvider = config.getString( FrameWorkPropertiesBean.DATA_PROVIDER ) ;
			}
			fwpb.setDataProvider( dataProvider ) ; 
			
			int uploadMaxSize = 40960000 ;
			if ( config.containsKey ( FrameWorkPropertiesBean.UPLOAD_MAX_SIZE ) ) {
				uploadMaxSize = config.getInt ( FrameWorkPropertiesBean.UPLOAD_MAX_SIZE ) ;
			}
			fwpb.setUploadMaxSize ( uploadMaxSize ) ;
			
			String uploadServerPath = "/upload" ;
			if (  config.containsKey ( FrameWorkPropertiesBean.UPLOAD_SERVER_PATH ) ) {
				uploadServerPath =  config.getString ( FrameWorkPropertiesBean.UPLOAD_SERVER_PATH ) ;
			}
			fwpb.setUploadServerPath ( uploadServerPath ) ;
			
			
			String frameworkLog = FrameWorkPropertiesBean.FRAMEWORK_NOLOG;
			if (  config.containsKey ( FrameWorkPropertiesBean.FRAMEWORK_LOG ) ) {
				frameworkLog =  config.getString ( FrameWorkPropertiesBean.FRAMEWORK_LOG ) ;
			}
			fwpb.setFrameworkLog (  frameworkLog ) ;
			
			
			/*
			String appServer = "TOMCAT" ; 
			if (  config.containsKey ( FrameWorkPropertiesBean.APP_SERVER ) ) {
				appServer =  config.getString ( FrameWorkPropertiesBean.APP_SERVER ) ;
			}
			fwpb.setAppServer ( appServer )  ;
			*/
		}
		catch ( Exception eq ) {
			logger.error(  "init config.properties ::" + eq.getMessage() , eq ) ;
		}
		logger.debug( "init app config end ..."  ) ;		
	}

	public Collection getInitBeans ( ) {
		return initBeans;
	}

	public void setInitBeans ( Collection initBeans ) {
		if ( initBeans != null  ) {
			//Assert.notEmpty( initBeans );
	        Iterator iter = initBeans.iterator();
			while ( iter.hasNext() ) {
				Object currentObject = iter.next();
				Assert.isInstanceOf( IAppInitBean.class, currentObject,
						"IAppInitBean " + currentObject.getClass().getName()
								+ " must implement IAppInitBean" );
			}			
		}
		this.initBeans = initBeans ;
	}
	
	
	private void initoBeans () throws Exception {
		logger.info( "init other beans start ..."  ) ;
		if ( initBeans != null ) {
			Iterator iter = initBeans.iterator() ; 
			while ( iter.hasNext() ) {
				Object currentObject = iter.next();
				IAppInitBean aib = ( IAppInitBean ) currentObject ;  
				aib.init();
			}			
		}
		logger.info( "init other beans end ..."  ) ;
	}
}
